package com.student.liu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.student.liu.entity.Orderform;
import com.student.liu.service.IOrderformService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 订单相关 接口
 */
@Slf4j
@RestController
public class OrderformController extends BaseController {

    @Resource
    IOrderformService orderformService;

    /**
     * 创建订单
     */
    @PostMapping("create")
    public Object create(@RequestBody CreateOrderformParam createOrderformParam) {
        createOrderformParam.setOrderformId(orderformService.createOrderform(createOrderformParam.getMoney(),
                createOrderformParam.getTableId()));
        return success("订单创建成功,返回订单ID", createOrderformParam.getOrderformId());
    }

    @PostMapping("orderpage")
    public Object page(@Valid @RequestBody OrderformController.OrderPageBody orderPageBody, BindingResult result) throws InterruptedException {
        IPage<Map<String, Object>> goodsMaps = orderformService.orderPage(orderPageBody.getPageIndex(), 5);
        return success("", goodsMaps);
    }

    @GetMapping("getById/{id}")
    public Object GetById(@PathVariable Integer id) {
        return success(null, orderformService.slectOrderform(id));
    }

    /**
     * 支付完成
     *
     * @return
     */
    @GetMapping("payMoney/{id}")
    public Object putOn(@PathVariable Integer id) {
        orderformService.update(id, (byte) 2);
        orderformService.addWaiterId(id);
        return success("支付完成", null);
    }

    @GetMapping("cancel/{id}")
    public Object cancel(@PathVariable Integer id) {
        orderformService.update(id, (byte) 3);
        return success("取消订单", null);
    }

    @PostMapping("remove")
    public Object remove(@RequestBody Orderform orderform) {
        orderformService.remove(orderform.getId());
        return success("删除未支付订单成功", null);
    }

    @Data
    static class CreateOrderformParam{
        Float money;    //总价格
        Integer tableId;      //座位号
        Integer orderformId;
    }

    @Data
    static class OrderPageBody {
        @NotNull(message = "请传递页码")
        private Integer pageIndex;
    }

}
